//
//  ShoppingListItem.m
//  Shopping List
//
//  Created by Tim Isted on 22/09/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ShoppingListItem.h"

NSString * const kShoppingListItemName = @"kShoppingListItemName";
NSString * const kShoppingListQuantity = @"kShoppingListQuantity";
NSString * const kShoppingListPurchased = @"kShoppingListPurchased";

@implementation ShoppingListItem

@synthesize quantity, itemName, purchased;

- (id)initWithName:(NSString *)newName quantity:(int)newQuantity
{
	if( self = [super init] )
	{
		itemName = [newName retain];
		quantity = newQuantity;
        purchased = NO;
	}
	
	return self;
}

+ (id)shoppingListItemWithName:(NSString *)newName quantity:(int)newQuantity
{
	return [[[ShoppingListItem alloc] initWithName:newName quantity:newQuantity] autorelease];
}

- (id)init
{
	return [self initWithName:@"Bread" quantity:1];
}

- (void)dealloc
{
	[itemName release];
	
	[super dealloc];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    NSLog(@"Coder (%@) = %@", [aCoder class], aCoder);   
    [aCoder encodeObject:itemName forKey:kShoppingListItemName];
    [aCoder encodeInt:quantity forKey:kShoppingListQuantity];
    [aCoder encodeBool:purchased forKey:kShoppingListPurchased];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if( self = [super init] )
	{
		itemName = [[aDecoder decodeObjectForKey:kShoppingListItemName] retain];
		quantity = [aDecoder decodeIntForKey:kShoppingListQuantity];
        purchased = [aDecoder decodeBoolForKey:kShoppingListPurchased];
	}
	
	return self;
}

@end
